package com.ninth_group.dao.Impl;

import com.ninth_group.dao.PawDao;
import com.ninth_group.po.City;
import com.ninth_group.utils.HiveUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

@Repository
public class PawDaoImpl implements PawDao {
    @Resource
    private HiveUtil hiveUtil;

    private static final String CID_SQL =
            "select city_id from provincial_capital_weather_info where province = ?";
    private static final String PAW_SQL = "select * from city_base_info where city_id = ?";

    /**
     * 获取省会天气信息
     * @param pName 省份名称
     * @return 省会天气信息
     */
    @Override
    public City getPawData(String pName) {
        // 通过省会表获取省会城市id
        String cid = getPid(pName);
        System.out.println("获取省会id: " + cid);

        // 通过城市id获取省会天气信息
        City city = hiveUtil.queryForObject(PAW_SQL, s -> {
            try {
                return City.mapRow(s);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }, cid);
        System.out.println("省会天气信息: " + city);
        return city;
    }

    /**
     * 通过省会名获取省会id
     * @param pName 省会名
     * @return pid
     */
    @Override
    public String getPid(String pName) {
        return hiveUtil.queryForString(CID_SQL, pName);
    }

}
